<?php 
/*
Here is a script that is usefull to :
- login to a POST form,
- store a session cookie,
- download a file once logged in.
*/
class Compete
{
	const COMPETE_LOGIN = 'https://my.compete.com/login/';
	const COMPETE_SITE = 'http://siteanalytics.compete.com/';
	const COMPETE_USR = COMPETE_USER_NAME;
	const COMPETE_PASS = COMPETE_PASSWORD;
	const CURL_TIMEOUT = 20;
	
	private $domain;
	public $compete_content;
	
	// data to retrieve
	private $unique_visitors = 0;
	private $visits = 0;
	private $page_views = 0;
	private $att_m = 0;
	private $att_d = 0;
	private $avg_s = 0;
	public  $page_v = 0;
	
	public function __construct($url)
	{
		$this->domain = $url;
	}
	public function getUrl($url)
	{
		$this->domain = $url;
	}
	public function process()
	{
		$unique = NULL;
		$visits = NULL;
		$page_views = NULL;
		$att_m = NULL;
		$att_d = NULL;
		$avg_s = NULL;
		$page_v = NULL;
		
		// <div class = "number value">145,948,025</div>
		$content = $this->compete_connect();
		
		if(!$content) return false;
		
		// get unique visitors, visits, etc 
		$regexp = '#<div class="number\s+(no-data|value)">(?<name>(.*?))</div>#si';
		
		preg_match_all($regexp, $content, $matches);
	    
		if (!isset($matches)) return false;
		
		$unique = $this->check_commas_numbers($matches['name'][0]);
		if(isset($unique) && !empty($unique))
		{
			$unique = str_replace(',', '', $unique);
			$this->unique_visitors = $unique + 0;		
		}
		
		$visits = $this->check_commas_numbers($matches['name'][1]);
		if(isset($visits) && !empty($visits))
		{
			$visits = str_replace(',', '', $visits);
			$this->visits = $visits + 0;;
		}
		
		$page_views = $this->check_commas_numbers($matches['name'][2]);
		if(isset($page_views) && !empty($page_views))
		{
			$page_views = str_replace(',', '', $page_views);
			$this->page_views =  $page_views + 0;
		}
		
		$att_m = $this->check_decimals_w_p($matches['name'][7]);
		if(isset($att_m) && !empty($att_m))
		{
			$att_m = str_replace('%', '', $att_m);
			$this->att_m = $att_m + 0;
		}
		
		$att_d = $this->check_decimals_w_p($matches['name'][9]);
		if(isset($att_d))
		{
			$att_d = str_replace('%', '', $att_d);
			$this->att_d = $att_d + 0;
		}
		
		$avg_s = $matches['name'][4];
		if(isset($avg_s) && !empty($avg_s))
		{
			$this->avg_s = $avg_s;
		}
		
		$page_v = $this->check_decimals($matches['name'][6]);
		if(isset($page_v) && !empty($page_v))
		{
			$this->page_v = $page_v  + 0;
		}
		// echo "</pre>";
		return true;
	}
	private function check_commas_numbers($s)
	{
		preg_match('/([\d]{1,3},?)*/',$s,$m);
		return $m[0];
	}
	private function check_decimals_w_p($s)
	{
		preg_match('/([\d]+\.[\d]+\s+%)/',$s,$m);
		return $m[0];
	}
	private function check_decimals($s)
	{
		preg_match('/([\d]+\.[\d]+)/',$s,$m);
		return $m[0];
	}
	public function unique()
	{
		return $this->unique_visitors;
	}
	public function visits()
	{
		return $this->visits;
	}
	public function page_views()
	{
		return $this->page_views;
	}
	public function page_visits()
	{
		return $this->page_v;
	}
	public function avg_stay()
	{
		return $this->avg_s;
	}
	public function att_d()
	{
		return $this->att_d;
	}
	public function att_m()
	{
		return $this->att_m;
	}
	private function compete_connect()
	{
		$cookie_jar = tempnam('/tmp','cookie');
		
		// INIT CURL
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, self::CURL_TIMEOUT);
		// SET URL FOR THE POST FORM LOGIN
		curl_setopt($ch, CURLOPT_URL, self::COMPETE_LOGIN);
		// ENABLE HTTP POST
		curl_setopt ($ch, CURLOPT_POST, 1);
		curl_setopt ($ch, CURLOPT_HEADER, 0);
		// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
		curl_setopt ($ch, CURLOPT_POSTFIELDS, 'l_email=' . self::COMPETE_USR . '&l_password=' . self:: COMPETE_PASS);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
		# not to print out the results of its query.
		# Instead, it will return the results as a string return value
		# from curl_exec() instead of the usual true/false.
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
		curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_jar);
		curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_jar);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
		// EXECUTE 1st REQUEST (FORM LOGIN)
		$store = curl_exec ($ch);
		
		if(!$store) return false;
		
		// SET FILE TO DOWNLOAD
		curl_setopt($ch, CURLOPT_URL, self::COMPETE_SITE . $this->domain);
		
		// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
		$content = curl_exec ($ch);
		$this->compete_content = $content;
		// CLOSE CURL
		curl_close ($ch);
		unset($ch);
		
		unlink($cookie_jar) or die ('There was an error in the script');
		
		return $this->compete_content;
	}
}
?>